package com.drugmanage.controller;

import com.drugmanage.Base.BaseController;
import com.drugmanage.annotations.Pms;
import com.drugmanage.constant.DmConstant;
import com.drugmanage.entity.User;
import com.drugmanage.model.ResultInfo;
import com.drugmanage.model.UserModel;
import com.drugmanage.query.UserQuery;
import com.drugmanage.service.IUserService;
import com.drugmanage.utils.LoginUserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController extends BaseController {
    @Resource
    private IUserService userService;

    @RequestMapping("toRegister")
    @ResponseBody
    public ResultInfo register(String userName, String mobile, String password) {
        User user = new User();
        user.setUsername(userName);
        user.setMobile(mobile);
        user.setPwd(password);
        userService.register(user);
        return success("注册成功", user);
    }

    @RequestMapping("login")
    @ResponseBody
    public ResultInfo login(String userName, String pwd, HttpSession session) {
        UserModel userModel =  userService.login(userName, pwd);
        //存入权限值
        session.setAttribute(DmConstant.USER_PERMISSION_KEY,userModel.getPermissions());
        return success("登录成功",userModel);
    }

    @RequestMapping("index")
    public String index() {
        return "index";
    }

    @RequestMapping("info")
    public String index2(){
        return "user";
    }

    @RequestMapping("updateUserPassword")
    @ResponseBody
    public ResultInfo updateUserPassword(HttpServletRequest request, String oldPassword, String newPassword, String confirmPassword) {
        Integer userId = LoginUserUtil.releaseUserIdFromCookie(request);
        userService.updateUserPassword(userId, oldPassword, newPassword, confirmPassword);
        return success("修改成功！");
    }

    @RequestMapping("main")
    public String main(HttpServletRequest request) {
        Integer userId = LoginUserUtil.releaseUserIdFromCookie(request);
        User user = userService.getById(userId);
        request.setAttribute("user", user);
        return "main";
    }

    @RequestMapping("queryUser")
    @ResponseBody
    public String queryUser(HttpServletRequest request){
        Integer id = LoginUserUtil.releaseUserIdFromCookie(request);
        return userService.queryUser(id);
    }

    @RequestMapping("queryUsersByParams")
    @ResponseBody
    @Pms(acl = "888802")
    public Map<String,Object> queryUserByParams(UserQuery userQuery){
        return userService.queryUserByParams(userQuery);
    }

    @RequestMapping("saveUser")
    @ResponseBody
    @Pms(acl = "88880201")
    public ResultInfo saveUser(User user){
        userService.saveUser(user);
        return success("添加成功");
    }

    @RequestMapping("updateUser")
    @ResponseBody
    @Pms(acl = "88880202")
    public ResultInfo updateUser(User user){
        userService.updateUser(user);
        return success("修改成功");
    }

    @RequestMapping("deleteUser")
    @ResponseBody
    @Pms(acl = "88880203")
    public ResultInfo deleteUser(Integer[] ids){
        userService.deleteUser(ids[0]);
        return success("删除成功");
    }

}
